// 
//  EVECDSkillPlan.m
//  EVE Mac Suite
//
//  Created by Vadim Lozko on 5/4/09.
//  Copyright 2009  __Vadim__. All rights reserved.
//

#import "EVECDSkillPlan.h"
#import "EVECDCharacter.h"
#import "EVECDSkillPlanItem.h"
#import "EVECDLearnedSkill.h"
#import "EVECDSkill.h"
#import "EVECDCharismaEnhancer.h"
#import "EVECDIntelligenceEnhancer.h"
#import "EVECDMemoryEnhancer.h"
#import "EVECDPerceptionEnhancer.h"
#import "EVECDWillpowerEnhancer.h"
#import "EVEAPICoreDataController.h"
#import "EMSGeneralUtils.h"

@implementation EVECDSkillPlan 

@dynamic name;
@dynamic planFor;
@dynamic skillPlanItems;

@end

@implementation EVECDSkillPlan (SkillPlannerExtras)

-(double)valueForAttributeWOLearning:(NSString*)attribute
{
	if([attribute isEqualToString:@"charisma"])
	{
		return charismaValueWOLearning;
	}
	else if([attribute isEqualToString:@"intelligence"])
	{
		return intelligenceValueWOLearning;
	}
	else if([attribute isEqualToString:@"memory"])
	{
		return memoryValueWOLearning;
	}
	else if([attribute isEqualToString:@"perception"])
	{
		return perceptionValueWOLearning;
	}
	else if([attribute isEqualToString:@"willpower"])
	{
		return willpowerValueWOLearning;
	} else {
		return 0;
	}
}

-(void)recalculateTrainingTimes
{
	NSArray* sortedSkillPlanItems = [[self.skillPlanItems allObjects] sortedArrayUsingDescriptors:[EMSGeneralUtils positionSortDescriptors]];
	EVECDCharacter* character = self.planFor;
	int learningLevel = 0;
	NSPredicate* learningSkillNamePredicate = [NSPredicate predicateWithFormat:@"skill.name like 'Learning'"];
	NSPredicate* characterNamePredicate = [NSPredicate predicateWithFormat:@"partOfLearnedGroup.learnedBy.name like %@", character.name];
	NSPredicate* learningSkillPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:
										   [NSArray arrayWithObjects:learningSkillNamePredicate, characterNamePredicate, nil]];
	EVECDLearnedSkill* learningSkill = (EVECDLearnedSkill*)[EVECDContext fetchObjectForEntityName:@"EVECDLearnedSkill" 
																					withPredicate:learningSkillPredicate];
	if(learningSkill)
	{
		learningLevel = learningSkill.level.intValue;
	}
	charismaValueWOLearning = character.charisma.doubleValue / (1 + (learningLevel * .02));
	intelligenceValueWOLearning = character.intelligence.doubleValue / (1 + (learningLevel * .02));
	memoryValueWOLearning = character.memory.doubleValue / (1 + (learningLevel * .02));
	perceptionValueWOLearning = character.perception.doubleValue / (1 + (learningLevel * .02));
	willpowerValueWOLearning = character.willpower.doubleValue / (1 + (learningLevel * .02));

	int i;
	for(i = 0; i < [sortedSkillPlanItems count]; i++)
	{
		EVECDSkillPlanItem* item = [sortedSkillPlanItems objectAtIndex:i];
		NSString* skillName = item.learnedSkill.skill.name;
		if([skillName isEqualToString:@"Empathy"])
		{
			item.primaryAttributeValue = [NSNumber numberWithDouble:(memoryValueWOLearning * (1 + (learningLevel * .02)))];
			item.secondaryAttributeValue = [NSNumber numberWithDouble:(intelligenceValueWOLearning * (1 + (learningLevel * .02)))];
			charismaValueWOLearning++;
		}
		else if([skillName isEqualToString:@"Presence"])
		{
			item.primaryAttributeValue = [NSNumber numberWithDouble:(charismaValueWOLearning * (1 + (learningLevel * .02)))];
			item.secondaryAttributeValue = [NSNumber numberWithDouble:(perceptionValueWOLearning * (1 + (learningLevel * .02)))];
			charismaValueWOLearning++;
		}
		else if([skillName isEqualToString:@"Analytical Mind"])
		{
			item.primaryAttributeValue = [NSNumber numberWithDouble:(memoryValueWOLearning * (1 + (learningLevel * .02)))];
			item.secondaryAttributeValue = [NSNumber numberWithDouble:(intelligenceValueWOLearning * (1 + (learningLevel * .02)))];
			intelligenceValueWOLearning++;
		}
		else if([skillName isEqualToString:@"Logic"])
		{
			item.primaryAttributeValue = [NSNumber numberWithDouble:(intelligenceValueWOLearning * (1 + (learningLevel * .02)))];
			item.secondaryAttributeValue = [NSNumber numberWithDouble:(memoryValueWOLearning * (1 + (learningLevel * .02)))];
			intelligenceValueWOLearning++;
		}
		else if([skillName isEqualToString:@"Eidetic Memory"])
		{
			item.primaryAttributeValue = [NSNumber numberWithDouble:(memoryValueWOLearning * (1 + (learningLevel * .02)))];
			item.secondaryAttributeValue = [NSNumber numberWithDouble:(intelligenceValueWOLearning * (1 + (learningLevel * .02)))];
			memoryValueWOLearning++;
		}
		else if([skillName isEqualToString:@"Instant Recall"])
		{
			item.primaryAttributeValue = [NSNumber numberWithDouble:(memoryValueWOLearning * (1 + (learningLevel * .02)))];
			item.secondaryAttributeValue = [NSNumber numberWithDouble:(intelligenceValueWOLearning * (1 + (learningLevel * .02)))];
			memoryValueWOLearning++;
		}
		else if([skillName isEqualToString:@"Clarity"])
		{
			item.primaryAttributeValue = [NSNumber numberWithDouble:(perceptionValueWOLearning * (1 + (learningLevel * .02)))];
			item.secondaryAttributeValue = [NSNumber numberWithDouble:(willpowerValueWOLearning * (1 + (learningLevel * .02)))];
			perceptionValueWOLearning++;
		}
		else if([skillName isEqualToString:@"Spatial Awareness"])
		{
			item.primaryAttributeValue = [NSNumber numberWithDouble:(memoryValueWOLearning * (1 + (learningLevel * .02)))];
			item.secondaryAttributeValue = [NSNumber numberWithDouble:(intelligenceValueWOLearning * (1 + (learningLevel * .02)))];
			perceptionValueWOLearning++;
		}
		else if([skillName isEqualToString:@"Iron Will"])
		{
			item.primaryAttributeValue = [NSNumber numberWithDouble:(memoryValueWOLearning * (1 + (learningLevel * .02)))];
			item.secondaryAttributeValue = [NSNumber numberWithDouble:(intelligenceValueWOLearning * (1 + (learningLevel * .02)))];
			willpowerValueWOLearning++;
		}
		else if([skillName isEqualToString:@"Focus"])
		{
			item.primaryAttributeValue = [NSNumber numberWithDouble:(willpowerValueWOLearning * (1 + (learningLevel * .02)))];
			item.secondaryAttributeValue = [NSNumber numberWithDouble:(charismaValueWOLearning * (1 + (learningLevel * .02)))];
			willpowerValueWOLearning++;
		}
		else if([skillName isEqualToString:@"Learning"])
		{
			item.primaryAttributeValue = [NSNumber numberWithDouble:(memoryValueWOLearning * (1 + (learningLevel * .02)))];
			item.secondaryAttributeValue = [NSNumber numberWithDouble:(intelligenceValueWOLearning * (1 + (learningLevel * .02)))];
			learningLevel++;
		} else {
			item.primaryAttributeValue = [NSNumber numberWithDouble:
										  ([self valueForAttributeWOLearning:item.learnedSkill.skill.primaryAttribute] * (1 + (learningLevel * .02)))];
			item.secondaryAttributeValue = [NSNumber numberWithDouble:
											([self valueForAttributeWOLearning:item.learnedSkill.skill.secondaryAttribute] * (1 + (learningLevel * .02)))];
		}
	}
}

-(NSNumber*)previousPlannedSkillFinishDateFromSkillPlanItem:(EVECDSkillPlanItem*)item
{
	NSArray* sortedSkillPlanItems = [[self.skillPlanItems allObjects] sortedArrayUsingDescriptors:[EMSGeneralUtils positionSortDescriptors]];
	int index = [sortedSkillPlanItems indexOfObject:item];
	if(index == 0)
	{
		return [NSNumber numberWithDouble:0];
	} else {
		return [[sortedSkillPlanItems objectAtIndex:(index - 1)] finishDate];
	}
}

@end